<?php
class Maverick_Acl extends Zend_Acl{
	
	public function __construct(){
		
		$users = new Users();
		$users = $users->getUsersNoAdmin();
    	
    	$modules = new Modules();
    	$modules = $modules->fetchAll("modules_id = 0")->toArray();
    	
    	$modNames = array();
    	foreach($modules as $m){
    		$modNames[$m['id']] = $m['name'];	
    	}
    	
    	$this->addRole(new Zend_Acl_Role('guest'));
    	$this->addRole(new Zend_Acl_Role('admin'));
    	
    	//$this->add(new Zend_Acl_Resource('index'));
    	//$this->add(new Zend_Acl_Resource('user'));
		foreach ($modules as $mod){
			$this->add(new Zend_Acl_Resource($mod['name']));
			$this->allow('admin',$mod['name']);
    	}
    	
    	foreach($users as $us){
    		$this->addRole(new Zend_Acl_Role($us['login']));
    	}
    	//asm
    	/*
    	$this->allow('admin',null);
    	$this->allow(null,'index');
    	$this->allow(null,'user');
*/

    	
    	$per = new Permissions();
    	$tabModules = array();
    	
    	foreach($users as $user){
    		$tabModules =  $per->getAdapter()->fetchAll("select permissions.modules_id from permissions where permissions.users_id =".$user['id']);
    		if (count($tabModules) > 0){
    			foreach($tabModules as $tab){
    				$this->allow($user['login'],$modNames[$tab['modules_id']]);
    			}
    		}
    	}
    	
    	//tablica z uzytkownikami
    	$tabUsers = $per->getAdapter()->fetchAll("select users.login from users, permissions where users.id = permissions.users_id");
    	//tablica z modulami
    	$tabModules =  $per->getAdapter()->fetchAll("select modules.name from modules, permissions where modules.id = permissions.modules_id");
    	
    	
	}
}
?>